package com.nova.edc.service.energy.impl;

import java.util.List;
import java.util.Map;

import com.nova.edc.model.energy.Energy;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.frame.dao.parsing.SQLParser;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.sql.SqlBuffer;
import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.dao.energy.SummaryDao;
import com.nova.edc.model.energy.Summary;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.SummaryService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.nova.module.filter.query.Filter;

public class SummaryServiceImpl implements SummaryService {

    @Autowired
    private SummaryDao novaSummaryDao;

    @Override
    public void insertOrUpdateSummary(List<Summary> summaries) {
        for (Summary summary : summaries) {
            Summary temp = getSummary(summary);
            if (temp != null) {
                temp.setActivestate(temp.getActivestate()
                        + summary.getActivestate());
                temp.setModifyby(ModelConstant.TIME_TASK);
                temp.setModifydate(DateUtils.getCurrentDate());
                novaSummaryDao.updateNotNull(temp);
            } else {
                summary.setAddby(ModelConstant.TIME_TASK);
                summary.setAdddate(DateUtils.getCurrentDate());
                novaSummaryDao.insert(summary);
            }
        }

    }

    @Override
    public Summary getSummary(Summary summary) {
        Summary result = novaSummaryDao.query()
                .add(Restrictions.eq("CompanyCode", summary.getCompanycode()))
                .add(Restrictions.eq("GatewayID", summary.getGatewayid()))
                .add(Restrictions.eq("SensorID", summary.getSensorid()))
                .add(Restrictions.eq("ChannelID", summary.getChannelid()))
                .add(Restrictions.eq("SummaryDate", summary.getSummarydate()))
                .get();
        return result;
    }

    @Override
    public PageList<Summary> findPageList(Page page, Summary summary,
                                          String sidx, String sord) {
        Query<Summary> query = novaSummaryDao.query().addOrder(
                Order.custom(sord, sidx));
        Filter.addFilterCondition(query, summary);
        return query.pageList(page);
    }

    @Override
    public int positionInProvince(CustomerInfo customerInfo, String startTime, String endTime) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("summarySQL.positionInProvince"), true);
        List<Map<String, Object>> maps = JdbcUtils.queryMap(sqlBuffer.getSql(), startTime, endTime,
                "%" + (customerInfo.getProvince() == null ? "" : customerInfo.getProvince()) + "%", customerInfo.getCompanycode());
        if (maps!=null&& maps.size()>0) {
            double rownum = new Double(maps.get(0).get("rownum").toString());
            return (int) rownum;
        } else {
        return 0;
        }

    }
}
